package com.lzw.service.impl;

import com.lzw.bean.Menu;
import com.lzw.bean.MenuNode;
import com.lzw.common.bean.Result;
import com.lzw.common.service.impl.BaseServiceImpl;
import com.lzw.mapper.MenuMapper;
import com.lzw.mapper.MenuNodeMapper;
import com.lzw.service.MenuNodeService;
import com.lzw.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/6/30 0030.
 */
@Service("menuNodeService")
public class MenuNodeServiceImpl extends BaseServiceImpl<MenuNodeMapper,MenuNode> implements MenuNodeService {


    @Override
    public Result insertAll(List<MenuNode> menuNodes) {
        if(baseMapper.insertAll(menuNodes)>=1){
            return  this.success("插入多条数据成功");
        }else{
            return  this.error("插入多条数据失败");
        }
    }

    @Override
    public Result deleteOther(int menu_id, int[] node_ids) {
        if(baseMapper.deleteOther(menu_id,node_ids)>=1){
            return  this.success("删除其他数据成功");
        }else{
            return  this.error("删除其他数据失败");
        }
    }

    @Override
    public Result setNode(int menu_id, int[] node_ids) {
        Result result1=this.deleteOther(menu_id,node_ids);
        Result result2=null;
        if(node_ids!=null&&node_ids.length>0){
            List<MenuNode> list=new ArrayList<>();
            for (int i=0;i<node_ids.length;i++){
                if(this.findByMenuIdNodeId(menu_id,node_ids[i])==null){
                    MenuNode menuNode=new MenuNode();
                    menuNode.setMenuId(menu_id);
                    menuNode.setNodeId(node_ids[i]);
                    list.add(menuNode);
                }

            }
            if(list.size()>0)
             result2=this.insertAll(list);
        }
        if(result1.getCode()==0||(result2!=null&&result2.getCode()==0)){
            return  this.success("授权成功！");
        }else{
            return  this.error("授权失败！");
        }

    }

    @Override
    public MenuNode findByMenuIdNodeId(int menu_id, int node_id) {
        return baseMapper.findByMenuIdNodeId(menu_id,node_id);
    }
}
